package org.csu.mypetstore.tools;

import sun.misc.BASE64Decoder;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class Base64ToImage {
    public static boolean Base64ToImage(String imgStr, String imgFilePath) {
        imgStr = imgStr.substring(imgStr.indexOf(',')+1);
        if ("".equals(imgStr) || imgStr == null)
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
